﻿using SlaughterItem.Common.DBContext;
using SlaughterItem.Common.Infrastructures.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Dapper;
using MySql.Data.MySqlClient;
using Pomelo.EntityFrameworkCore.MySql.Migrations.Internal;
using Microsoft.Extensions.Configuration;
using static System.Runtime.InteropServices.JavaScript.JSType;
using THE_WIND_RISES.Common.Domans.Produce.Customers;
using Microsoft.Data.SqlClient;
using System.Configuration;
using THE_WIND_RISES.Common.Domans.RBAC;
namespace SlaughterItem.Common.Infrastructures.Repositories
{
    /// <summary>
    /// 基类仓储
    /// </summary>
    /// <typeparam name="T">占位符</typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : class ,new()
    {
        private readonly IDbConnection Db;
        private readonly IConfiguration _configuration;
        public BaseRepository(IConfiguration configuration)
        {
            _configuration = configuration;
            Db = new MySqlConnection(_configuration.GetConnectionString("Whq")); ;
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns>所有数据</returns>
        public IEnumerable<T> GetAll(string sql, object param = null)
        {
            try
            {
                var list = Db.Query<T>(sql,param);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public int Add(string sql, object param)  
        {
            try
            {
                var res = Db.Execute(sql, param);
                return res;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public int AddRound(string sql,object param)
        {
            try
            {
                var res = Db.Execute(sql, param);
                return res;
            }
            catch (Exception)
            {
                throw;
            }
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public int Update(string sql, object Data)
        {
            try
            {
                var res = Db.Execute(sql, Data);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int Delete(string sql,object Data)
        {
            try
            {
                var res = Db.Execute(sql, Data);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 事务操作
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="Data"></param>
        /// <returns></returns>
        public int AffairCRUD(List<string> SqlList, object? Data=null)
        {
            var result = 0;
            Db.Open();
            using (IDbTransaction transaction = Db.BeginTransaction())
            {
                try
                {
                    foreach (var item in SqlList)
                    {
                        result = Db.Execute(item, Data);
                    }
                    //提交事务
                    transaction.Commit();
                }
                catch (Exception)
                {
                    //回滚事务
                    transaction.Rollback();
                    throw;
                }
            }
            return result;
        }
    }
}
